package com.ccjq.jq173.product;

import com.ccjq.jq173.core.util.UuidUtils;
import com.ccjq.jq173.product.po.Category;
import com.ccjq.jq173.product.po.PageInfo;
import com.ccjq.jq173.product.po.Product;
import com.ccjq.jq173.product.service.ProductService;
import com.ccjq.jq173.product.service.impl.ProductServiceimpl;
import com.ccjq.jq173.shopping.service.ShoppingCartService;
import com.ccjq.jq173.shopping.service.impl.ShoppingCartServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

public class ProductServlet extends HttpServlet {
    private ProductService service;
    private ShoppingCartService shop;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入商品Servlets");
        String flag=request.getParameter("flag");
        System.out.println("输入的"+flag);
        service=new ProductServiceimpl();
        //产品列表
        ArrayList<Category> a=(ArrayList<Category>)service.queryCategorys();
        request.setAttribute("category",a);//添加左侧商品栏
        //热卖
        ArrayList<Product>hot=(ArrayList<Product>)service.queryHotSales();
        request.setAttribute("hot",hot);
        //推荐
        ArrayList<Product>tui=(ArrayList<Product>)service.queryNominateProducts();
        request.setAttribute("tui",tui);

        if(StringUtils.isBlank(flag))
        {
            System.out.println("flag="+flag);
            String zt=request.getParameter("zt");
            String pageSize=request.getParameter("pageSize");
            String pageNo=request.getParameter("pageNo");
            String userId=request.getParameter("userId");
            String categoryId=request.getParameter("categoryId");
            String pages=request.getParameter("pages");
            System.out.println(pageSize+","+pageNo+","+userId);
            if(!StringUtils.isBlank(zt))
            {
                if(zt.equals("next"))
                {
                    if(Integer.parseInt(pageNo)+1>Integer.parseInt(pages))
                    {
                        pageNo=Integer.toString(Integer.parseInt(pages));
                    }
                    else
                    {
                        pageNo=Integer.toString(Integer.parseInt(pageNo)+1);
                    }

                }
                if(zt.equals("up"))
                {
                    if(Integer.parseInt(pageNo)-1==0)
                    {
                        pageNo=Integer.toString(1);
                    }
                    else
                    {
                        pageNo=Integer.toString(Integer.parseInt(pageNo)-1);
                    }
                }
            }
            if(StringUtils.isBlank(pageSize)){
                pageSize="6";
            }
            if(StringUtils.isBlank(pageNo)){
                pageNo="1";
            }

            PageInfo<Product> p=service.queryProducts(categoryId,Integer.parseInt(pageSize),Integer.parseInt(pageNo));
            System.out.println(p.getObjs().size());
            request.setAttribute("pageinfo",p);
            request.setAttribute("category",a);
            if(StringUtils.isBlank(categoryId))
            {
                request.setAttribute("category_id",null);
                request.setAttribute("category_name","全部");
            }
            if(!StringUtils.isBlank(categoryId))
            {
                String category_id=null;
                String name=null;
                if(p.getObjs().size()>0)
                {
                    category_id=p.getObjs().get(1).getCategoryId();
                    name=p.getObjs().get(1).getCategoryName();
                }
                else
                {
                    name="商品目前未有上架";
                }
                request.setAttribute("category_name",name);
                request.setAttribute("category_id",category_id);
            }
            request.getRequestDispatcher("/WEB-INF/jsp/product/produce_list.jsp").forward(request,response);
            return;
        }
        ////////////////////////////////////////////////////////////////
        if(flag.equals("d"))
        {
            String productId=request.getParameter("productId");
            Product p=service.queryProductsDetail(productId);
            request.setAttribute("Product",p);//添加商品详情
            System.out.println(p.getCategoryId()+"112 ...........112 "+p.getCategoryName());
            request.getRequestDispatcher("/WEB-INF/jsp/product/produce_detail.jsp").forward(request,response);
            return;
        }
        if(flag.equals("add"))
        {
            shop=new ShoppingCartServiceImpl();
            String productId=request.getParameter("productId");
            int quantity=Integer.parseInt(request.getParameter("quantity"));
            String user_id=request.getParameter("user_id");
            System.out.println(productId+","+quantity+","+user_id);
            String shopping_id= UuidUtils.generateShortUUID("U",10);
            System.out.println(quantity+", "+shopping_id+","+user_id+","+productId);
            int k=shop.saveShoppingCart(quantity,shopping_id,user_id,productId);
            response.getWriter().write(k+"");
            //判断该商品是否加入过购物车
        /*    if(!service.pdjl(productId,user_id))
            {
                System.out.println("------------1");
                int kcl_stock=service.getStockByProductId(productId);//该商品目前的总库存量
                if(quantity<=kcl_stock)
                {
                    int num=kcl_stock-quantity;
                    service.updateStack(productId,num);
                    if(service.addsp(productId,quantity,user_id))
                    {
                        response.getWriter().write("11成功");
                    }
                    else
                    {
                        response.getWriter().write("12失败");
                    }
                }

            }
            else
            {
                System.out.println("------------2");
                int kcl_stock=service.getStockByProductId(productId);//该商品目前的总库存量
                System.out.println("总数量"+kcl_stock);
                System.out.println("购买"+quantity);
                int num=kcl_stock-quantity;
                if(quantity<=kcl_stock)
                {
                    service.updateStack(productId,num);
                    service.updateCar(productId,quantity,user_id);
                    response.getWriter().write("21成功");
                }
                else
                {
                    response.getWriter().write("目前商品火爆，库存超出您的选择");
                }
            }*/
        }


        //   PageInfo<Order> pageInfo=service.queryOrders(userId,Integer.parseInt(pageSize),Integer.parseInt(pageNo));
        //   System.out.println(pageSize+","+pageNo+","+userId+"bbbbb");
        //   request.setAttribute("pageInfo",pageInfo);
        //   System.out.println(pageSize+","+pageNo+","+userId+"ccccc");
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
